﻿using Medical.Api.Write.Application.Command.Wms.WmsLssueCommand;
using Medical.Api.Write.Application.Command.Wms.WmsOutCommand;
using Medical.Api.Write.Application.Command.Wms.WmsStoresCommand;
using Medical.Api.Write.Dto.Wms;
using Medical.Domain.Enum;
using Medical.Domain.Wms;
using Medical.ErrorCode;
using Medical.Interstructrue;
using Medical.Interstructrue.Base;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static Medical.Domain.Enum.DepartmentEnum;

namespace Medical.Api.Write.Service.WmsService
{
    public class WmsService : IWmsService
    {
        private readonly IBaseRepository<WmsStores> _storesRepository;
        private readonly IBaseRepository<WmsOut> _outRepository;
        private readonly IBaseRepository<WmsOutDetail> _outDetailRepository;
        private readonly MedicalDbContext _dbContext;
        public WmsService(
            IBaseRepository<WmsStores> storesRepository,
            IBaseRepository<WmsOut> outRepository,
            IBaseRepository<WmsOutDetail> outDetailRepository,
            MedicalDbContext dbContext)
        {
            _storesRepository = storesRepository;
            _outRepository = outRepository;
            _outDetailRepository = outDetailRepository;
            _dbContext = dbContext;
        }
        public async Task<ApiResult<bool>> MaterialStoreShelveAsync(StoreShelveCommand command)
        {
            var store = _storesRepository.GetModel(command.Id);
            if (store == null)
            {
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "物品不存在", Data = false };
            }
            // 校验同货架、层、格且未删除的格位是否已存物品
            bool slotExists = _storesRepository.GetAll().Any(s =>
                s.ShelfId == command.ShelfId &&
                s.FloorNum == command.FloorNum &&
                s.LatticeNum == command.LatticeNum &&
                !s.IsDeleted &&
                s.Id != command.Id // 排除自己
            );
            if (slotExists)
            {
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "该格位已存入物品，请重新选择", Data = false };
            }
            store.ShelfId = command.ShelfId;
            store.FloorNum = command.FloorNum;
            store.LatticeNum = command.LatticeNum;
            store.StoresNum = command.StoresNum;
            store.StoresState = true;
            var result = _storesRepository.Update(store);
            return await Task.FromResult(new ApiResult<bool>
            {
                Code = result > 0 ? ApiEnums.Success : ApiEnums.Fail,
                Msg = result > 0 ? "存放成功" : "存放失败",
                Data = result > 0
            });
        }
        /// <summary>
        /// 出库审批（状态0改为5）
        /// </summary>
        public async Task<ApiResult<bool>> AuditOutAsync(int id)
        {
            var entity = _outRepository.GetModel(id);
            if (entity == null)
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "出库单不存在", Data = false };
            if ((int)entity.AuditState == 5)
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "已审批", Data = false };
            entity.AuditState = (ApprovalStateEnum)5;
            var result = _outRepository.Update(entity);
            return await Task.FromResult(new ApiResult<bool>
            {
                Code = result > 0 ? ApiEnums.Success : ApiEnums.Fail,
                Msg = result > 0 ? "审批成功" : "审批失败",
                Data = result > 0
            });
        }
        /// <summary>
        /// 出库撤销审批（状态5改为0）
        /// </summary>
        public async Task<ApiResult<bool>> UndoAuditOutAsync(int id)
        {
            var entity = _outRepository.GetModel(id);
            if (entity == null)
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "出库单不存在", Data = false };
            if ((int)entity.AuditState == 0)
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "未审批", Data = false };
            entity.AuditState = (Medical.Domain.Enum.ApprovalStateEnum)0;
            var result = _outRepository.Update(entity);
            return await Task.FromResult(new ApiResult<bool>
            {
                Code = result > 0 ? ApiEnums.Success : ApiEnums.Fail,
                Msg = result > 0 ? "撤销成功" : "撤销失败",
                Data = result > 0
            });
        }
        /// <summary>
        /// 删除出库单，仅状态为0时允许删除，主子表逻辑删除，带异常处理和事务回滚
        /// </summary>
        public async Task<ApiResult<bool>> DeleteOutAsync(int id)
        {
            using var tran = _dbContext.Database.BeginTransaction(); // 启动事务
            try
            {
                var entity = _outRepository.GetModel(id);
                if (entity == null)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "出库单不存在", Data = false };
                if ((int)entity.AuditState != 0)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "仅未审批状态可删除", Data = false };
                entity.IsDeleted = true;
                var details = _outDetailRepository.GetAll().Where(x => x.OutId == id && !x.IsDeleted).ToList();
                foreach (var d in details)
                {
                    d.IsDeleted = true;
                }
                // 批量更新明细表
                _outDetailRepository.UpdateRange(details);
                var result = _outRepository.Update(entity);
                if (result > 0)
                {
                    tran.Commit(); // 提交事务
                    return new ApiResult<bool>
                    {
                        Code = ApiEnums.Success,
                        Msg = "删除成功",
                        Data = true
                    };
                }
                else
                {
                    tran.Rollback();
                    return new ApiResult<bool>
                    {
                        Code = ApiEnums.Fail,
                        Msg = "删除失败",
                        Data = false
                    };
                }
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"删除异常: {ex.Message}",
                    Data = false
                };
            }
        }
        /// <summary>
        /// 修改出库主表，仅允许未审批（AuditState=0）时修改仓库和原因
        /// </summary>
        public async Task<ApiResult<bool>> UptOutAsync(UptOutDto dto)
        {
            var entity = _outRepository.GetModel(dto.Id);
            if (entity == null)
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "出库单不存在", Data = false };
            if ((int)entity.AuditState != 0)
                return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "仅未审批状态可修改", Data = false };
            entity.OutWarehouse = (Medical.Domain.Enum.WarehouseEnum)dto.OutWarehouse;
            entity.OutReason = (Medical.Domain.Enum.OutCauseEnum)dto.OutReason;
            var result = _outRepository.Update(entity);
            return await Task.FromResult(new ApiResult<bool>
            {
                Code = result > 0 ? ApiEnums.Success : ApiEnums.Fail,
                Msg = result > 0 ? "修改成功" : "修改失败",
                Data = result > 0
            });
        }
        /// <summary>
        /// 新增发放主子表，带事务
        /// </summary>
        public async Task<ApiResult<bool>> AddLssueAsync(AddLssueDto dto)
        {
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {
                var entity = new WmsLssue
                {
                    LssueCode = dto.LssueCode,
                    LssueName = dto.LssueName,
                    LssueDate = dto.LssueDate,
                    //DepaId = dto.DepaId,
                    ApplyingUnit = (ApplyingUnitEnum)dto.ApplyingUnit,
                    AcceptName = dto.AcceptName,
                    LssueState = (Medical.Domain.Enum.LssueTypeEnunm)dto.LssueState
                };
                _dbContext.WmsLssues.Add(entity);
                _dbContext.SaveChanges();
                var details = dto.Details.Select(d => new WmsLssueDetail
                {
                    LssueId = entity.Id,
                    MaterialType = (Medical.Domain.Enum.MaterialTypeEnum)d.MaterialType,
                    MaterialId = d.MaterialId,
                    MaterialCode = d.MaterialCode,
                    MaterialName = d.MaterialName,
                    LssueNum = d.LssueNum
                }).ToList();
                _dbContext.WmsLssueDetails.AddRange(details);
                _dbContext.SaveChanges();
                tran.Commit();
                return new ApiResult<bool> { Code = ApiEnums.Success, Msg = "新增成功", Data = true };
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return new ApiResult<bool> { Code = ApiEnums.Error, Msg = $"新增异常: {ex.Message}", Data = false };
            }
        }
        /// <summary>
        /// 发放主子表逻辑删除，仅状态为0时允许删除，主表和明细表IsDeleted=true，带事务和异常处理
        /// </summary>
        /// <param name="id">发放主表Id</param>
        /// <returns>删除结果</returns>
        public async Task<ApiResult<bool>> DeleteLssueAsync(int id)
        {
            // 启动数据库事务，确保主表和明细表同时逻辑删除
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {
                // 查询主表，要求未被删除
                var entity = _dbContext.WmsLssues.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
                if (entity == null)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "发放单不存在", Data = false };
                // 仅未发放状态（LssueState=0）允许删除
                if ((int)entity.LssueState != 0)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "仅未发放状态可删除", Data = false };
                // 逻辑删除主表
                entity.IsDeleted = true;
                // 查询并逻辑删除所有明细
                var details = _dbContext.WmsLssueDetails.Where(x => x.LssueId == id && !x.IsDeleted).ToList();
                foreach (var d in details)
                {
                    d.IsDeleted = true;
                }
                // 批量更新明细表
                _dbContext.WmsLssueDetails.UpdateRange(details);
                // 更新主表
                _dbContext.WmsLssues.Update(entity);
                // 保存更改
                _dbContext.SaveChanges();
                // 提交事务
                tran.Commit();
                return new ApiResult<bool> { Code = ApiEnums.Success, Msg = "删除成功", Data = true };
            }
            catch (Exception ex)
            {
                // 发生异常时回滚事务，保证数据一致性
                tran.Rollback();
                return new ApiResult<bool> { Code = ApiEnums.Error, Msg = $"删除异常: {ex.Message}", Data = false };
            }
        }
        /// <summary>
        /// 发放主表签收，签收时扣减存放表库存，校验库存充足，否则提示物品库存不足
        /// </summary>
        public async Task<ApiResult<bool>> SignLssueAsync(int id)
        {
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {
                // 1. 获取发放主表
                var entity = _dbContext.WmsLssues.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
                if (entity == null)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "发放单不存在", Data = false };
                if ((int)entity.LssueState != 0)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "仅未发放状态可签收", Data = false };
                // 2. 获取发放明细ids
                var lssueDetails = _dbContext.WmsLssueDetails.Where(d => d.LssueId == id && !d.IsDeleted).ToList();
                if (lssueDetails.Count == 0)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "发放明细不存在", Data = false };
                // 3. 获取所有出库Id
                var outIds = lssueDetails.Select(d => d.OutId).Distinct().ToList();
                // 4. 获取所有出库明细ids
                var outDetailList = _dbContext.WmsOutDetails.Where(d => outIds.Contains(d.OutId) && !d.IsDeleted).ToList();
                var outDetailIds = outDetailList.Select(d => d.Id).ToList();
                // 5. 获取所有存放表ids
                var storesIds = outDetailList.Select(d => d.StoresId).Distinct().ToList();
                var storesList = _dbContext.WmsStores.Where(s => storesIds.Contains(s.Id) && !s.IsDeleted).ToList();
                // 6. 校验库存并扣减
                foreach (var lssueDetail in lssueDetails)
                {
                    // 找到对应出库明细
                    var outDetail = outDetailList.FirstOrDefault(d => d.OutId == lssueDetail.OutId && d.MaterialId == lssueDetail.MaterialId);
                    if (outDetail == null)
                        return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = $"未找到出库明细，物品ID:{lssueDetail.MaterialId}", Data = false };
                    // 找到对应存放表
                    var store = storesList.FirstOrDefault(s => s.Id == outDetail.StoresId);
                    if (store == null)
                        return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = $"未找到存放表，物品ID:{lssueDetail.MaterialId}", Data = false };
                    int storeNum = store.StoresNum ?? 0;
                    int lssueNum = lssueDetail.LssueNum;
                    if (storeNum < lssueNum)
                        return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = $"{lssueDetail.MaterialName ?? lssueDetail.MaterialId.ToString()}物品库存不足，请先联系供应商进货", Data = false };
                    if (storeNum == lssueNum)
                    {
                        // 数量相等，逻辑删除存放表数据，相当于全部出库，清空货架
                        store.StoresNum = 0;
                        store.StoresState = false;
                        store.IsDeleted = true;
                    }
                    else
                    {
                        store.StoresNum = storeNum - lssueNum;
                    }
                    _dbContext.WmsStores.Update(store);
                }
                // 7. 更新发放主表状态
                entity.LssueState = (Medical.Domain.Enum.LssueTypeEnunm)5;
                _dbContext.WmsLssues.Update(entity);
                _dbContext.SaveChanges();
                tran.Commit();
                return new ApiResult<bool> { Code = ApiEnums.Success, Msg = "签收成功", Data = true };
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return new ApiResult<bool> { Code = ApiEnums.Error, Msg = $"签收异常: {ex.Message}", Data = false };
            }
        }
        /// <summary>
        /// 通过入库主键Id批量添加存放数据，StorageId = StorageId，ShelfId = null，FloorNum = null，LatticeNum = null，StoresNum = StorageNum，MaterialType = MaterialType，StoresState = false
        /// </summary>
        public async Task<ApiResult<bool>> BatchAddStoresFromStorageAsync(int storageId)
        {
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {
                // 校验入库主表状态，必须已审批（AuditState=true）
                var storage = _dbContext.WmsStorages.FirstOrDefault(s => s.Id == storageId && !s.IsDeleted);
                if (storage == null)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "未找到入库主表", Data = false };
                if (!storage.AuditState)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "仅已审批状态可添加到存放表", Data = false };
                // 校验存放表中是否已存在该StorageId
                bool exists = _dbContext.WmsStores.Any(s => s.StorageId == storageId && !s.IsDeleted);
                if (exists)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "该入库单已生成存放单，请勿重复操作", Data = false };
                // 获取入库明细
                var details = _dbContext.WmsStorageDetails.Where(d => d.StorageId == storageId).ToList();
                if (details.Count == 0)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "未找到入库明细", Data = false };
                // 转为存放数据
                var storesList = details.Select(d => new WmsStores
                {
                    StorageId = d.StorageId,
                    ShelfId = null,
                    FloorNum = null,
                    LatticeNum = null,
                    StoresNum = d.StorageNum,
                    MaterialId = d.MaterialId,
                    MaterialType = d.MaterialType,
                    StoresState = false
                }).ToList();
                _dbContext.WmsStores.AddRange(storesList);
                _dbContext.SaveChanges();
                tran.Commit();
                return new ApiResult<bool> { Code = ApiEnums.Success, Msg = "批量添加存放数据成功", Data = true };
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return new ApiResult<bool> { Code = ApiEnums.Error, Msg = $"批量添加存放数据异常: {ex.Message}", Data = false };
            }
        }
        /// <summary>
        /// 从存放表选择明细新增出库单，出库单号=CK+yyMMddHHmmss，AuditState=0，明细存放id为选择的存放id，出库数量不能大于存放数量
        /// </summary>
        public async Task<ApiResult<bool>> AddOutFromStoresAsync(AddOutFromStoresCommand command)
        {
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {

                // 校验明细
                var storesIds = command.Details.Select(d => d.StoresId).ToList();
                var storesList = _dbContext.WmsStores.Where(s => storesIds.Contains(s.Id) && !s.IsDeleted).ToList();
                if (storesList.Count != command.Details.Count)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "部分存放数据不存在", Data = false };
                // 校验出库数量不能大于存放数量
                foreach (var detail in command.Details)
                {
                    var store = storesList.First(s => s.Id == detail.StoresId);
                    if (detail.OutNum > (store.StoresNum ?? 0))
                        return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = $"出库数量不能大于存放数量，存放Id:{detail.StoresId}", Data = false };
                }
                // 生成出库单号 CK+yyMMddHHmmss
                string outCode = "CK" + DateTime.Now.ToString("yyMMddHHmmss");
                // 新增出库主表
                var outEntity = new WmsOut
                {
                    OutCode = outCode,
                    OutWarehouse = (WarehouseEnum)command.OutWarehouse,
                    OutReason = (OutCauseEnum)command.OutReason,
                    ApplyingUnit = (ApplyingUnitEnum)command.ApplyingUnit,
                    AuditState = ApprovalStateEnum.登记,
                    AuditName = command.AuditName,
                    AuditDate = DateTime.MinValue,
                    CreateTime = DateTime.Now
                };

                _dbContext.WmsOuts.Add(outEntity);
                _dbContext.SaveChanges();

                // 获取所有MaterialId，用于批量查询器械和器械包信息
                var materialIds = storesList.Select(s => s.MaterialId).Distinct().ToList();

                // 批量查询器械信息
                var apparatusList = _dbContext.Apparatuses
                    .Where(a => materialIds.Contains(a.Id) && !a.IsDeleted)
                    .ToDictionary(a => a.Id, a => new { Code = a.ApparatusCode, Name = a.ApparatusName });

                // 批量查询器械包信息
                var packageList = _dbContext.EquipmentPackages
                    .Where(p => materialIds.Contains(p.Id) && !p.IsDeleted)
                    .ToDictionary(p => p.Id, p => new { Code = p.PackageCode, Name = p.PackageName });

                // 新增出库明细
                var outDetails = command.Details.Select(detail =>
                {
                    var store = storesList.First(s => s.Id == detail.StoresId);
                    string materialCode = string.Empty;
                    string materialName = string.Empty;

                    // 根据MaterialType动态获取MaterialCode和MaterialName
                    if (store.MaterialType == MaterialTypeEnum.Instrument) // MaterialType = 0，查询器械表
                    {
                        if (apparatusList.TryGetValue(store.MaterialId, out var apparatus))
                        {
                            materialCode = apparatus.Code;
                            materialName = apparatus.Name;
                        }
                    }
                    else if (store.MaterialType == MaterialTypeEnum.器械包) // MaterialType = 5，查询器械包表
                    {
                        if (packageList.TryGetValue(store.MaterialId, out var package))
                        {
                            materialCode = package.Code;
                            materialName = package.Name;
                        }
                    }

                    return new WmsOutDetail
                    {
                        OutId = outEntity.Id,
                        MaterialType = store.MaterialType,
                        MaterialId = store.MaterialId,
                        MaterialCode = materialCode,
                        MaterialName = materialName,
                        OutNum = detail.OutNum,
                        StoresId = store.Id
                    };
                }).ToList();


                _dbContext.WmsOutDetails.AddRange(outDetails);
                _dbContext.SaveChanges();
                tran.Commit();
                return new ApiResult<bool> { Code = ApiEnums.Success, Msg = "新增出库单成功", Data = true };
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return new ApiResult<bool> { Code = ApiEnums.Error, Msg = $"新增出库单异常: {ex.Message}", Data = false };
            }
        }
        /// <summary>
        /// 根据出库单主键Id新增发放单，主表和明细字段按用户要求
        /// </summary>
        /// <param name="command">包含出库单Id、发放人、接收人等参数</param>
        /// <returns>操作结果</returns>
        /// <remarks>
        /// 业务流程：
        /// 1. 校验出库单存在且状态为5（已审批）
        /// 2. 校验发放明细表中是否已存在该出库Id，防止重复生成
        /// 3. 获取出库明细，校验存在性
        /// 4. 生成发放单号（FF+yyMMddHHmmss）
        /// 5. 新增发放主表（LssueCode、LssueName、LssueDate=DateTime.MinValue、ApplyingUnit、AcceptName、LssueState=0）
        /// 6. 新增发放明细（LssueId、MaterialType、MaterialId、MaterialCode、MaterialName、LssueNum=OutNum、OutId=出库Id）
        /// 7. 所有操作带事务，异常自动回滚
        /// </remarks>
        public async Task<ApiResult<bool>> AddLssueFromOutAsync(AddLssueFromOutCommand command)
        {
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {
                // 获取出库主表
                var outEntity = _dbContext.WmsOuts.FirstOrDefault(x => x.Id == command.OutId && !x.IsDeleted);
                if (outEntity == null)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "未找到出库主表", Data = false };
                // 仅出库状态为5（已审批）时才能生成发放单
                if ((int)outEntity.AuditState != 5)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "仅已审批的出库单可生成发放单", Data = false };
                // 校验发放明细表中是否已存在该出库Id，防止重复生成
                bool exists = _dbContext.WmsLssueDetails.Any(d => d.OutId == command.OutId);
                if (exists)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "当前出库单已生成发放单，请勿重复操作", Data = false };
                // 获取出库明细
                var outDetails = _dbContext.WmsOutDetails.Where(d => d.OutId == command.OutId && !d.IsDeleted).ToList();
                if (outDetails.Count == 0)
                    return new ApiResult<bool> { Code = ApiEnums.Fail, Msg = "未找到出库明细", Data = false };
                // 生成发放单号 FF+yyMMddHHmmss
                string lssueCode = "FF" + DateTime.Now.ToString("yyMMddHHmmss");
                // 新增发放主表
                var lssue = new WmsLssue
                {
                    LssueCode = lssueCode,
                    LssueName = command.LssueName, // 审核人
                    LssueDate = DateTime.Now, // 当前时间
                    ApplyingUnit = outEntity.ApplyingUnit,
                    AcceptName = command.AcceptName, // 创建人
                    LssueState = LssueTypeEnunm.已发放,
                    
                };
                _dbContext.WmsLssues.Add(lssue);
                _dbContext.SaveChanges();
                // 新增发放明细
                var lssueDetails = outDetails.Select(detail => new WmsLssueDetail
                {
                    LssueId = lssue.Id,
                    MaterialType = detail.MaterialType,
                    MaterialId = detail.MaterialId,
                    MaterialCode = detail.MaterialCode,
                    MaterialName = detail.MaterialName,
                    LssueNum = detail.OutNum,
                    OutId = outEntity.Id
                }).ToList();
                _dbContext.WmsLssueDetails.AddRange(lssueDetails);
                _dbContext.SaveChanges();
                // 新增发放主子表成功后，修改出库表状态为10（接收）
                outEntity.AuditState = ApprovalStateEnum.接收; // 10=接收
                _dbContext.WmsOuts.Update(outEntity);
                _dbContext.SaveChanges();
                tran.Commit();
                return new ApiResult<bool> { Code = ApiEnums.Success, Msg = "新增发放单成功，出库单状态已更新为接收", Data = true };
            }
            catch (Exception ex)
            {
                // 发生异常时回滚事务，保证数据一致性
                tran.Rollback();
                return new ApiResult<bool> { Code = ApiEnums.Error, Msg = $"新增发放单异常: {ex.Message}", Data = false };
            }
        }
    }
}
